var util = require('../../utils/util.js');
//获取应用实例
const app = getApp();
Page({
  data: {
    //tabbar
    tabbar: {},
    motto: 'Hello World',
    avatarUrl: '',
    nickName: '',
    hasUserInfo: false,
  },

  onShow: function() {
    //隐藏系统tabbar
    wx.hideTabBar();
    this.getUserInfo();
  },
  onLoad: function() {

    app.editTabbar();
    this.setData({
      avatarUrl: app.globalData.getUserInfo.avatarUrl,
      nickName: app.globalData.getUserInfo.nickName
    })
    


  },
  getUserInfo: function() {
    var that = this;
    util.wGet('user/info', {}).then((res) => {
      var resData = res.data.data;
      that.setData({
        joinCount: resData.join_count,
        joinWinCount: resData.join_win_count,
        activityCount: resData.activity_count
      })
    })
  },
  joinList: function() {
    wx.navigateTo({
      url: '../joinlist/joinlist'
    })
  },
  createList: function() {
    wx.navigateTo({
      url: '../createlist/createlist'
    })
  },
  winList: function() {
    wx.navigateTo({
      url: '../winlist/winlist'
    })
  },
  commonProblem: function() {
    wx.navigateTo({
      url: '../commonProblem/commonProblem'
    })
  },
  busCoo: function() {
    wx.navigateTo({
      url: '../busCoo/busCoo'
    })
  },
//   地址授权
  addressShouq:function(){
	  var that=this;
	  // 判断如果用户拒绝授权
	  wx.getSetting({//先获取用户当前的设置
		  success(res) {
			  if (!res.authSetting['scope.address']) {
				  wx.authorize({
					  scope: 'scope.address',
					  success(res) {
						//   console.log(res.errMsg);
						  //用户授权后执行方法
					  },
					  fail(res) {
						  //用户拒绝授权后执行
						  wx.showModal({
							  title: '提示',
							  content: '请允许授权地址获取更多权限',
							  success(res) {
								  if (res.confirm) {
									  that.address();
								  } else if (res.cancel) {
								  }
							  }
						  })
					  }
				  })
			  }else{
				  //打开选择地址
				  wx.chooseAddress({
					  success: function (res) {

						  util.wPost('address/update', {
							  name: res.userName,
							  mobile: res.telNumber,
							  code: res.nationalCode || res.postalCode,
							  address: res.provinceName + res.cityName + res.countyName + res.detailInfo
						  }).then((res) => {
							  wx.showToast({
								  icon: "none",
								  title: '成功'
							  })
						  })
					  }
				  })
			  }
		  }
	  })
  },
  // 收货地址
  address: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.openSetting({ });
        } else {
          //打开选择地址
          wx.chooseAddress({
            success: function (res) {
              
              util.wPost('address/update', {
                name: res.userName,
                mobile: res.telNumber,
                code: res.nationalCode || res.postalCode,
                address: res.provinceName + res.cityName + res.countyName + res.detailInfo
              }).then((res) => {
                wx.showToast({
                  icon: "none",
                  title: '成功'
                })
              })
            }
          })
        }
      },
      fail(res) {
        // console.log('调用失败')
      }
    })
    // wx.chooseAddress({
    //   success(res) {
    //     util.wPost('address/update', {
    //       name: res.userName,
    //       mobile: res.telNumber,
    //       code: res.nationalCode || res.postalCode,
    //       address: res.provinceName + res.cityName + res.countyName + res.detailInfo
    //     }).then((res) => {
    //       wx.showToast({
    //         icon: "none",
    //         title: '成功'
    //       })
    //     })
    //   }
    // })
  }
//   客服消息
	, handleContact(e) {
		// console.log(e.path)
		// console.log(e.query)
	}
	// 预览图片
	, bindViewTap: function (e) {
		var index = e.currentTarget.dataset.index;
		var urls = [index];
		wx.previewImage({
			//当前显示图片
			current: index,
			//所有图片
			urls: urls
		})
	}
})